package com.vogue.meeting.common.security.service;

import com.vogue.meeting.common.security.dao.MeetingSecurityDao;
import com.vogue.meeting.common.security.model.Oauth2User;
import com.vogue.meeting.fluent.entity.UserEntity;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class Oauth2UserDetailsService implements UserDetailsService {

    @Resource
    private MeetingSecurityDao meetingSecurityDao;

    /**
     * 账号登录
     * @param account
     * @return
     */
    @Override
    public Oauth2User loadUserByUsername(String account) throws UsernameNotFoundException {
        UserEntity userEntity = meetingSecurityDao.loadUserByAccount(account);
        if (userEntity==null){
            throw new UsernameNotFoundException("没有找到用户");
        }
        // 判断成功后返回用户细节
        Oauth2User oauth2User = new Oauth2User();
        oauth2User.setAccount(userEntity.getAccount());
        oauth2User.setId(userEntity.getUserId());
        oauth2User.setNickName(userEntity.getNickName());
        oauth2User.setEnable(userEntity.getEnable());
        oauth2User.setLocked(userEntity.getLocked());
        oauth2User.setPassword(userEntity.getPassword());


        return oauth2User;
    }
}
